// 公共方法调用区
//	console.log("start")
/*if (!window.console || !console.firebug){
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
} */
	/**
	 * 获取数据
	 * @param url
	 * @param opation
	 * @param htmlurl
	 * @param scripturl
	 * @param cookies
	 * @param isLog
	 */
	function geetdata(url, opation, htmlurl, scripturl, cookies, isLog, callback) {
		$.getJSON(url, opation, function(json){
			// 日志打印模块
//			if(isLog) {
//				console.log("在geetdata函数中opation:");
//				console.info(opation);
//				console.log("在geetdata函数中接收的json.data");
//				console.info(json.data);
//				console.info("在geetdata函数中接收的json数据是");
//				console.log(json);
//			}

			  view(json,  htmlurl, scripturl); // 负责界面显示数据
			  if(cookies != '') {
				  opation.pageCount = json.data.pageCount;
				  opation.total = json.data.total; 
//				  console.log(opation);
				  saveOpation(cookies, JSON.stringify(opation));   // 负责保存查询数据条件信息
			  }
			  
			  if(callback) {
				  callback();
			  }
			 
		})
	}
	function view(json, htmlurl, scripturl) {
		var $varietyContent = $(htmlurl),
	  	varietyHtml = template(scripturl, json);
		$varietyContent.html(varietyHtml);
		
	}
	
	function saveOpation(name, value) {
		$.cookie(name,value,{path:'/'});
	}
	/**
	 * 首页
	 */
	function fristPage(name, url, htmlurl, scripturl, cookies, isLog) {
		var value = $.cookie(name);
		var opation = JSON.parse(value);
			opation.pageNo = 1;
		geetdata(url, opation, htmlurl, scripturl, name, isLog);
		console.log("在fristPage函数中opation的内容是")
		console.info(opation);
	}
	/**
	 * 上一页
	 * @param name
	 * @param url
	 * @param htmlurl
	 * @param scripturl
	 * @param cookies
	 * @param isLog
	 */
	function pre(name, url, htmlurl, scripturl, cookies, isLog) {
		var value = $.cookie(name);
		var opation = JSON.parse(value);
		if(opation.pageNo > 1) {
			opation.pageNo = opation.pageNo - 1;
		}
		geetdata(url, opation, htmlurl, scripturl, name, isLog);
		console.log("在pre函数中opation的内容是")
		console.info(opation);
	}
	/**
	 * 跳到n页
	 */
	function toPage(pageNo, name, url, htmlurl, scripturl, cookies, isLog) {
		var value = $.cookie(name);
		var opation = JSON.parse(value);
		opation.pageNo = pageNo;
		
		geetdata(url, opation, htmlurl, scripturl, name, isLog);
		console.log("在toPage函数中opation的内容是")
		console.info(opation);
	}
	/**
	 * 下一页
	 */
	function next(name, url, htmlurl, scripturl, cookies, isLog) {
		var value = $.cookie(name);
		var opation = JSON.parse(value);
		if(opation.total%opation.pageSize == 0) {
			opation.pageCount = opation.total/opation.pageSize;
		}
		if(opation.pageNo < opation.pageCount) {
			opation.pageNo = opation.pageNo + 1;
		}
		
		geetdata(url, opation, htmlurl, scripturl, name, isLog, null);
		console.log("在next函数中opation的内容是")
		console.info(opation);
	}
	/**
	 * 最后一页
	 */
	function lastPage(name, url, htmlurl, scripturl, cookies, isLog) {
		var value = $.cookie(name);
		var opation = JSON.parse(value);
		alert(value)
		opation.pageNo = opation.pageCount;
		geetdata(url, opation, htmlurl, scripturl, name, isLog);
		console.log("在lastPage函数中opation的内容是")
		console.info(opation);
	}
	/**
	 * 获取图片地址
	 */
	function geetImg(imgUrl, n) {
		if(imgUrl == '') {
			return null;
		}
		if(n=='') {
			return imgUrl;
		}
		var imgurls = new Array();
		imgurls = imgUrl.split(',');
		if(n>imgurls.length+1) {
			alert('传递的参数有误！');
		} else {
			return imgurls[n-1];
		}
		return '';
	}
	template.helper("geetImgByIndex", geetImg);
	
	/**
	 * 打开新页面
	 * @param url 网页地址
	 * @param isNew 是否是新窗口打开
	 */
	function openUrl(url , isNew){
		console.log(url);
		if(isNew) {
			window.open(url);
		} else {
			window.location.href = url;
		}
	
	
	}
	
	/**
	 * 获取参数值
	 */
	function geetOpation() {
		var url=location.search;
    	var Request = new Object();
    	if(url.indexOf("?")!=-1){
    	    var str = url.substr(1);//去掉?号
    	    strs = str.split("&");
    	    for(var i=0;i<strs.length;i++){
    	        Request[strs[i ].split("=")[0]]=unescape(strs[i].split("=")[1]);
    	    }
    	}
    	return Request;
    	console.log(id);
	}
	
	
	function printPage(oData, n, m, url, add) {
		var result = '';
		alert(n)
		for(var i=0; i<n; i++) {
			result += "<a href='javascript:;' onclick='"+url+(oData + i)+"'>"+(oData +i) + "</a>";
		}
		$(add).html(result);
//		 return result;
	}
	template.helper("printPage", printPage);
	
	
	function wordLimit(content, n) {
		if(content == '') {
			return null;
		}else if(content.length < n){
			return content;
		} else {
			return content.substring(0,n)+"...";
		}
	}
	template.helper("wordLimit", wordLimit);
//	console.log("end");
	
	/**
	 * 调用示例：
	 * 1.改变图片选取位置
	 	
		<img src="{{value.imgUrl | geetImgByIndex:'1'}}" alt="图片"/>
	 */
	//bShare分享  
	var iBShare = {  
	    //初始化  
	    init: function() {  
	        var $shareBox = $(".bshare-custom");  
	        //加载分享工具  
	        var tools = ''; 
		        tools = '<a title="分享到QQ空间" class="bshare-qzone"></a>';  
		        tools += '<a title="分享到腾讯微博" class="bshare-qqmb"></a>';  
		        tools += '<a title="分享到新浪微博" class="bshare-sinaminiblog"></a>'; 
		        tools += '<a title="分享到微信" class="bshare-weixin"></a>';
//		        tools += '<a title="分享到人人网" class="bshare-renren"></a>';  
//		        tools += '<a title="分享到网易微博" class="bshare-neteasemb"></a>';
//		        tools += '<a title="更多平台" class="bshare-more bshare-more-icon more-style-addthis"></a>';  
	        $shareBox.append(tools);  
	        //绑定分享事件  
	        $shareBox.children("a").click(function() {  
	            var parents = $(this).parent();  
	            bShare.addEntry({  
	                title: parents.attr("title"),  
	                url: parents.attr("url"),  
	                summary: parents.attr("summary"),  
	                pic: parents.attr("pic")  
	            });  
	        });  
	    }  
	}  
	